using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using EcoTourEduca.BingYourMaps.Models;

namespace EcoTourEduca.BingYourMaps.DataMigrations {
    public class Migrations : DataMigrationImpl {

        public int Create() 
        {
            // Creating table BingMapsAreaRecord
            SchemaBuilder.CreateTable("BingMapsAreaRecord", table => table
                .ContentPartRecord()
                //.Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
                .Column("CenterLatitude", DbType.Double)
                .Column("CenterLongitude", DbType.Double)
                .Column("FriendlyName", DbType.String)
                .Column("MapHeight", DbType.Int32)
                .Column("MapWidth", DbType.Int32, column => column.Nullable())
                .Column("MapZoom", DbType.Int32)
                .Column("MapType", DbType.String, column => column.WithLength(4))
            );

            // Creating table BingMapsLocationRecord
            SchemaBuilder.CreateTable("BingMapsLocationRecord", table => table
                .ContentPartRecord()
                //.Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
                .Column("FriendlyName", DbType.String)
                .Column("Latitude", DbType.Double)
                .Column("Longitude", DbType.Double)
                .Column("MapHeight", DbType.Int32)
                .Column("MapWidth", DbType.Int32, column => column.Nullable())
                .Column("MapType", DbType.String, column => column.WithLength(4))
                .Column("MapZoom", DbType.Int32)
                .Column("PinIcon", DbType.String)
                .Column("HideMap", DbType.Boolean)
            );

            // Creating table BingMapsLocationsAreasMappingRecord (mapping table between BingMapsLocation and BingMapsArea records)
            SchemaBuilder.CreateTable("BingMapsLocationAreaMappingRecord", table => table
                .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
                .Column("BingMapsAreaRecord_Id", DbType.Int32)
                .Column("BingMapsLocationRecord_Id", DbType.Int32)
            );

            // Creating BingMapsAreaPart (attachable)
            ContentDefinitionManager.AlterPartDefinition(
                typeof(BingMapsAreaPart).Name
                , builder => builder.Attachable());

            // Creating BingMapsLocationPart (attachable)
            ContentDefinitionManager.AlterPartDefinition(
                typeof(BingMapsLocationPart).Name,
                builder => builder.Attachable()
            );

            return 1;
        }

        //public int UpdateFrom1()
        //{
        //    // Making BingMapsAreaPart attachable
        //    ContentDefinitionManager.AlterPartDefinition(
        //        "BingMapsAreaPart",
        //        builder => builder.Attachable()
        //    );

        //    // Making BingMapsLocationPart attachable
        //    ContentDefinitionManager.AlterPartDefinition(
        //        "BingMapsLocationPart",
        //        builder => builder.Attachable()
        //    );

        //    return 2;
        //}
    }
}